<template>
    <view>
        <youlanSignIn @shift="shift" :integral="integral" :isIntegral="isIntegral" :checkinDays="checkinDays" :already="data"
            :supplementary="true" type="sign" lang="zh" @changeMonth="historysign" @change="signDate" />

        <!-- 添加签到按钮 -->
        <button @click="signIn">签到</button>
    </view>
</template>

<script>
    import youlanSignIn from '@/components/youlan-SignIn/youlan-SignIn.vue'
    import * as index from '@/api/index.js' // 假设接口方法在这里

    export default {
        components: {
            youlanSignIn
        },
        onLoad() {
            // this.historysigncount()
            // this.gettask()
            // this.signIn() // 签到
            this.getSignInHistory() // 获取签到历史记录
            this.getMonthlyPoint() // 获取积分签到日历
        },
        mounted() {
            this.historysign()
        },
        data() {
            return {
                data: [],
                checkinDays: 0, // 连续签到天数
                integral: 0, // 本次签到获取的积分
                isIntegral: false, // 是否显示签到积分模块    
                signInHistory: [], // 签到历史记录
                monthlyPoint: [] // 积分签到日历
            };
        },
        methods: {
            shift() {
                console.log('用户点击了补签按钮');
            },
            async historysign() {
                this.data = []
                const res = {
                    code: 200,
                    data: {
                        "2024-09-01": "0",
                        "2024-09-02": "1",
                        "2024-09-03": "1",
                        "2024-09-04": "0",
                        "2024-09-05": "0",
                        "2024-09-06": "0",
                        "2024-09-07": "0",
                        "2024-09-08": "0",
                        "2024-09-09": "0",
                        "2024-09-10": "0",
                        "2024-09-31": "1",
                    }
                }

                if (res.code == 200) {
                    for (const i in res.data) {
                        if (res.data[i] == "1") {
                            console.log(i);
                            this.data.push(new Date(i).getTime())
                        }
                    }

                    console.log(this.data);
                } else this.$refs.uToast.show({
                    title: '获取签到信息失败',
                    type: 'error',
                })
            },
            async signDate(v) {
                console.log(v);
                this.checkinDays = this.checkinDays + 1;
            },
            // 积分签到
            async signIn() {
                const userId = uni.getStorageSync('userinfo').userId; // 假设用户id存储在本地存储中
                try {
                    const res = await index.signIn({ userId });
                    if (res.code === 0) {
                        this.integral = res.data.point;
                        this.isIntegral = true;
                        uni.showToast({
                            title: '签到成功',
                            icon: 'success'
                        });
                    } else {
                        uni.showToast({
                            title: '签到失败',
                            icon: 'none'
                        });
                    }
                } catch (err) {
                    console.error(err);
                }
            },
            // 签到历史记录
            async getSignInHistory() {
                const userId = uni.getStorageSync('userinfo').userId; // 假设用户id存储在本地存储中
                try {
                    const res = await index.signInPointList({ userId });
                    if (res.code === 0) {
                        this.signInHistory = res.data;
                    } else {
                        console.error('获取签到历史记录失败');
                    }
                } catch (err) {
                    console.error(err);
                }
            },
            // 积分签到日历
            async getMonthlyPoint() {
                const userId = uni.getStorageSync('userinfo').userId; // 假设用户id存储在本地存储中
                const yearMonth = new Date().toISOString().slice(0, 7); // 获取当前年月
                try {
                    const res = await index.monthlyPoint({ userId, yearMonth });
                    if (res.code === 200) {
                        this.monthlyPoint = res.data;
                    } else {
                        console.error('获取积分签到日历失败');
                    }
                } catch (err) {
                    console.error(err);
                }
            }
        }
    }
</script>